package com.eworking.eim.ui.utils;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Locale;

import android.text.TextUtils;

import com.eworking.util.EWoringLog;

public class HanziToPinyin
{
  private static final String TAG = "HanziToPinyin";
  private static final boolean DEBUG = false;
  public static final char[] UNIHANS = { 38463, '哎', '安', 32942, '凹', '八', '挀', '扳', 37030, '勹', 38466, '奔', '伻', '屄', 36793, '灬', '憋', '汃', '冫', '癶', '峬', '嚓', '偲', '参', '仓', '撡', '冊', '嵾', '曽', '曾', '層', '叉', 33414, 36799, '伥', '抄', 36710, '抻', '沈', '沉', 38455, '吃', '充', '抽', '出', '欻', '揣', '巛', '刅', '吹', '旾', 36916, '呲', '匆', '凑', '粗', '汆', '崔', 37032, '搓', '咑', '呆', '丹', '当', '刀', '嘚', '扥', '灯', '氐', '嗲', '甸', '刁', '爹', '丁', '丟', '东', '吺', '厾', 32785, 35176, '吨', '多', '妸', 35830, '奀', 38821, '儿', '发', '帆', '匚', 39134, '分', '丰', 35205, '仏', '紑', '伕', '旮', '侅', '甘', '冈', '皋', '戈', '给', '根', '刯', '工', '勾', '估', '瓜', '乖', '关', '光', '归', '丨', '呙', '哈', '咍', '佄', '夯', 33568, 35779, 40658, '拫', '亨', '噷', '叿', 40769, '乯', 33457, '怀', '犿', '巟', '灰', '昏', '吙', '丌', '加', '戋', '江', 33405, 38454, '巾', '坕', '冂', '丩', '凥', '姢', '噘', '军', '咔', '开', '刊', '忼', '尻', '匼', 32910, '劥', '空', '抠', '扝', '夸', 33967, '宽', '匡', '亏', '坤', '扩', '垃', '来', '兰', '啷', '捞', 32907, '勒', '崚', '刕', '俩', '奁', 33391, '撩', '列', '拎', '刢', '溜', '囖', 40857, '瞜', '噜', '娈', '畧', '抡', '罗', '呣', '妈', '埋', '嫚', '牤', '猫', '么', '呅', 38376, '甿', '咪', '宀', '喵', '乜', '民', '名', 35884, '摸', '哞', '毪', '嗯', '拏', 33097, '囡', '囔', '孬', '疒', '娞', '恁', 33021, '妮', '拈', '嬢', 40479, '捏', '囜', '宁', '妞', '农', '羺', '奴', '奻', '疟', 40641, 37069, '喔', 35764, '妑', '拍', '眅', '乓', '抛', '呸', '喷', '匉', '丕', '囨', '剽', '氕', '姘', '乒', 38027, '剖', '仆', '七', '掐', '千', '呛', '悄', '癿', '亲', '狅', 33422, '丘', '区', '峑', '缺', '夋', '呥', '穣', '娆', '惹', '人', '扔', '日', 33592, '厹', 37018, '挼', '堧', '婑', '瞤', '捼', '仨', '毢', '三', '桒', '掻', 38314, '森', '僧', '杀', '筛', '山', '伤', '弰', '奢', '申', 33688, '敒', '升', '尸', '収', '书', '刷', 34928, 38377, '双', 35841, '吮', 35828, '厶', '忪', '捜', 33487, '狻', '夊', '孙', '唆', '他', '囼', '坍', '汤', '夲', '忑', '熥', '剔', '天', '旫', '帖', '厅', '囲', '偷', '凸', '湍', '推', '吞', '乇', '穵', '歪', '弯', '尣', '危', '昷', '翁', '挝', '乌', '夕', 34418, '仚', '乡', '灱', '些', '心', '星', '凶', '休', '吁', '吅', '削', '坃', '丫', '恹', '央', '幺', '倻', '一', '囙', '应', '哟', '佣', '优', '扜', '囦', '曰', '晕', '筠', '筼', '帀', '災', '兂', '匨', '傮', '则', 36156, '怎', '増', '扎', '捚', '沾', '张', 38271, 38263, '佋', 34567, 36126, '争', '之', '峙', '庢', '中', '州', '朱', '抓', '拽', '专', '妆', 38585, '宒', '卓', '乲', '宗', 37049, '租', 38075, '厜', '尊', '昨', '兙', 40899, 40900 };
  public static final byte[][] PINYINS = { { 65 }, { 65, 73 }, { 65, 78 }, { 65, 78, 71 }, { 65, 79 }, { 66, 65 }, { 66, 65, 73 }, { 66, 65, 78 }, { 66, 65, 78, 71 }, { 66, 65, 79 }, { 66, 69, 73 }, { 66, 69, 78 }, { 66, 69, 78, 71 }, { 66, 73 }, { 66, 73, 65, 78 }, { 66, 73, 65, 79 }, { 66, 73, 69 }, { 66, 73, 78 }, { 66, 73, 78, 71 }, { 66, 79 }, { 66, 85 }, { 67, 65 }, { 67, 65, 73 }, { 67, 65, 78 }, { 67, 65, 78, 71 }, { 67, 65, 79 }, { 67, 69 }, { 67, 69, 78 }, { 67, 69, 78, 71 }, { 90, 69, 78, 71 }, { 67, 69, 78, 71 }, { 67, 72, 65 }, { 67, 72, 65, 73 }, { 67, 72, 65, 78 }, { 67, 72, 65, 78, 71 }, { 67, 72, 65, 79 }, { 67, 72, 69 }, { 67, 72, 69, 78 }, { 83, 72, 69, 78 }, { 67, 72, 69, 78 }, { 67, 72, 69, 78, 71 }, { 67, 72, 73 }, { 67, 72, 79, 78, 71 }, { 67, 72, 79, 85 }, { 67, 72, 85 }, { 67, 72, 85, 65 }, { 67, 72, 85, 65, 73 }, { 67, 72, 85, 65, 78 }, { 67, 72, 85, 65, 78, 71 }, { 67, 72, 85, 73 }, { 67, 72, 85, 78 }, { 67, 72, 85, 79 }, { 67, 73 }, { 67, 79, 78, 71 }, { 67, 79, 85 }, { 67, 85 }, { 67, 85, 65, 78 }, { 67, 85, 73 }, { 67, 85, 78 }, { 67, 85, 79 }, { 68, 65 }, { 68, 65, 73 }, { 68, 65, 78 }, { 68, 65, 78, 71 }, { 68, 65, 79 }, { 68, 69 }, { 68, 69, 78 }, { 68, 69, 78, 71 }, { 68, 73 }, { 68, 73, 65 }, { 68, 73, 65, 78 }, { 68, 73, 65, 79 }, { 68, 73, 69 }, { 68, 73, 78, 71 }, { 68, 73, 85 }, { 68, 79, 78, 71 }, { 68, 79, 85 }, { 68, 85 }, { 68, 85, 65, 78 }, { 68, 85, 73 }, { 68, 85, 78 }, { 68, 85, 79 }, { 69 }, { 69, 73 }, { 69, 78 }, { 69, 78, 71 }, { 69, 82 }, { 70, 65 }, { 70, 65, 78 }, { 70, 65, 78, 71 }, { 70, 69, 73 }, { 70, 69, 78 }, { 70, 69, 78, 71 }, { 70, 73, 65, 79 }, { 70, 79 }, { 70, 79, 85 }, { 70, 85 }, { 71, 65 }, { 71, 65, 73 }, { 71, 65, 78 }, { 71, 65, 78, 71 }, { 71, 65, 79 }, { 71, 69 }, { 71, 69, 73 }, { 71, 69, 78 }, { 71, 69, 78, 71 }, { 71, 79, 78, 71 }, { 71, 79, 85 }, { 71, 85 }, { 71, 85, 65 }, { 71, 85, 65, 73 }, { 71, 85, 65, 78 }, { 71, 85, 65, 78, 71 }, { 71, 85, 73 }, { 71, 85, 78 }, { 71, 85, 79 }, { 72, 65 }, { 72, 65, 73 }, { 72, 65, 78 }, { 72, 65, 78, 71 }, { 72, 65, 79 }, { 72, 69 }, { 72, 69, 73 }, { 72, 69, 78 }, { 72, 69, 78, 71 }, { 72, 77 }, { 72, 79, 78, 71 }, { 72, 79, 85 }, { 72, 85 }, { 72, 85, 65 }, { 72, 85, 65, 73 }, { 72, 85, 65, 78 }, { 72, 85, 65, 78, 71 }, { 72, 85, 73 }, { 72, 85, 78 }, { 72, 85, 79 }, { 74, 73 }, { 74, 73, 65 }, { 74, 73, 65, 78 }, { 74, 73, 65, 78, 71 }, { 74, 73, 65, 79 }, { 74, 73, 69 }, { 74, 73, 78 }, { 74, 73, 78, 71 }, { 74, 73, 79, 78, 71 }, { 74, 73, 85 }, { 74, 85 }, { 74, 85, 65, 78 }, { 74, 85, 69 }, { 74, 85, 78 }, { 75, 65 }, { 75, 65, 73 }, { 75, 65, 78 }, { 75, 65, 78, 71 }, { 75, 65, 79 }, { 75, 69 }, { 75, 69, 78 }, { 75, 69, 78, 71 }, { 75, 79, 78, 71 }, { 75, 79, 85 }, { 75, 85 }, { 75, 85, 65 }, { 75, 85, 65, 73 }, { 75, 85, 65, 78 }, { 75, 85, 65, 78, 71 }, { 75, 85, 73 }, { 75, 85, 78 }, { 75, 85, 79 }, { 76, 65 }, { 76, 65, 73 }, { 76, 65, 78 }, { 76, 65, 78, 71 }, { 76, 65, 79 }, { 76, 69 }, { 76, 69, 73 }, { 76, 69, 78, 71 }, { 76, 73 }, { 76, 73, 65 }, { 76, 73, 65, 78 }, { 76, 73, 65, 78, 71 }, { 76, 73, 65, 79 }, { 76, 73, 69 }, { 76, 73, 78 }, { 76, 73, 78, 71 }, { 76, 73, 85 }, { 76, 79 }, { 76, 79, 78, 71 }, { 76, 79, 85 }, { 76, 85 }, { 76, 85, 65, 78 }, { 76, 85, 69 }, { 76, 85, 78 }, { 76, 85, 79 }, { 77 }, { 77, 65 }, { 77, 65, 73 }, { 77, 65, 78 }, { 77, 65, 78, 71 }, { 77, 65, 79 }, { 77, 69 }, { 77, 69, 73 }, { 77, 69, 78 }, { 77, 69, 78, 71 }, { 77, 73 }, { 77, 73, 65, 78 }, { 77, 73, 65, 79 }, { 77, 73, 69 }, { 77, 73, 78 }, { 77, 73, 78, 71 }, { 77, 73, 85 }, { 77, 79 }, { 77, 79, 85 }, { 77, 85 }, { 78 }, { 78, 65 }, { 78, 65, 73 }, { 78, 65, 78 }, { 78, 65, 78, 71 }, { 78, 65, 79 }, { 78, 69 }, { 78, 69, 73 }, { 78, 69, 78 }, { 78, 69, 78, 71 }, { 78, 73 }, { 78, 73, 65, 78 }, { 78, 73, 65, 78, 71 }, { 78, 73, 65, 79 }, { 78, 73, 69 }, { 78, 73, 78 }, { 78, 73, 78, 71 }, { 78, 73, 85 }, { 78, 79, 78, 71 }, { 78, 79, 85 }, { 78, 85 }, { 78, 85, 65, 78 }, { 78, 85, 69 }, { 78, 85, 78 }, { 78, 85, 79 }, { 79 }, { 79, 85 }, { 80, 65 }, { 80, 65, 73 }, { 80, 65, 78 }, { 80, 65, 78, 71 }, { 80, 65, 79 }, { 80, 69, 73 }, { 80, 69, 78 }, { 80, 69, 78, 71 }, { 80, 73 }, { 80, 73, 65, 78 }, { 80, 73, 65, 79 }, { 80, 73, 69 }, { 80, 73, 78 }, { 80, 73, 78, 71 }, { 80, 79 }, { 80, 79, 85 }, { 80, 85 }, { 81, 73 }, { 81, 73, 65 }, { 81, 73, 65, 78 }, { 81, 73, 65, 78, 71 }, { 81, 73, 65, 79 }, { 81, 73, 69 }, { 81, 73, 78 }, { 81, 73, 78, 71 }, { 81, 73, 79, 78, 71 }, { 81, 73, 85 }, { 81, 85 }, { 81, 85, 65, 78 }, { 81, 85, 69 }, { 81, 85, 78 }, { 82, 65, 78 }, { 82, 65, 78, 71 }, { 82, 65, 79 }, { 82, 69 }, { 82, 69, 78 }, { 82, 69, 78, 71 }, { 82, 73 }, { 82, 79, 78, 71 }, { 82, 79, 85 }, { 82, 85 }, { 82, 85, 65 }, { 82, 85, 65, 78 }, { 82, 85, 73 }, { 82, 85, 78 }, { 82, 85, 79 }, { 83, 65 }, { 83, 65, 73 }, { 83, 65, 78 }, { 83, 65, 78, 71 }, { 83, 65, 79 }, { 83, 69 }, { 83, 69, 78 }, { 83, 69, 78, 71 }, { 83, 72, 65 }, { 83, 72, 65, 73 }, { 83, 72, 65, 78 }, { 83, 72, 65, 78, 71 }, { 83, 72, 65, 79 }, { 83, 72, 69 }, { 83, 72, 69, 78 }, { 88, 73, 78 }, { 83, 72, 69, 78 }, { 83, 72, 69, 78, 71 }, { 83, 72, 73 }, { 83, 72, 79, 85 }, { 83, 72, 85 }, { 83, 72, 85, 65 }, { 83, 72, 85, 65, 73 }, { 83, 72, 85, 65, 78 }, { 83, 72, 85, 65, 78, 71 }, { 83, 72, 85, 73 }, { 83, 72, 85, 78 }, { 83, 72, 85, 79 }, { 83, 73 }, { 83, 79, 78, 71 }, { 83, 79, 85 }, { 83, 85 }, { 83, 85, 65, 78 }, { 83, 85, 73 }, { 83, 85, 78 }, { 83, 85, 79 }, { 84, 65 }, { 84, 65, 73 }, { 84, 65, 78 }, { 84, 65, 78, 71 }, { 84, 65, 79 }, { 84, 69 }, { 84, 69, 78, 71 }, { 84, 73 }, { 84, 73, 65, 78 }, { 84, 73, 65, 79 }, { 84, 73, 69 }, { 84, 73, 78, 71 }, { 84, 79, 78, 71 }, { 84, 79, 85 }, { 84, 85 }, { 84, 85, 65, 78 }, { 84, 85, 73 }, { 84, 85, 78 }, { 84, 85, 79 }, { 87, 65 }, { 87, 65, 73 }, { 87, 65, 78 }, { 87, 65, 78, 71 }, { 87, 69, 73 }, { 87, 69, 78 }, { 87, 69, 78, 71 }, { 87, 79 }, { 87, 85 }, { 88, 73 }, { 88, 73, 65 }, { 88, 73, 65, 78 }, { 88, 73, 65, 78, 71 }, { 88, 73, 65, 79 }, { 88, 73, 69 }, { 88, 73, 78 }, { 88, 73, 78, 71 }, { 88, 73, 79, 78, 71 }, { 88, 73, 85 }, { 88, 85 }, { 88, 85, 65, 78 }, { 88, 85, 69 }, { 88, 85, 78 }, { 89, 65 }, { 89, 65, 78 }, { 89, 65, 78, 71 }, { 89, 65, 79 }, { 89, 69 }, { 89, 73 }, { 89, 73, 78 }, { 89, 73, 78, 71 }, { 89, 79 }, { 89, 79, 78, 71 }, { 89, 79, 85 }, { 89, 85 }, { 89, 85, 65, 78 }, { 89, 85, 69 }, { 89, 85, 78 }, { 74, 85, 78 }, { 89, 85, 78 }, { 90, 65 }, { 90, 65, 73 }, { 90, 65, 78 }, { 90, 65, 78, 71 }, { 90, 65, 79 }, { 90, 69 }, { 90, 69, 73 }, { 90, 69, 78 }, { 90, 69, 78, 71 }, { 90, 72, 65 }, { 90, 72, 65, 73 }, { 90, 72, 65, 78 }, { 90, 72, 65, 78, 71 }, { 67, 72, 65, 78, 71 }, { 90, 72, 65, 78, 71 }, { 90, 72, 65, 79 }, { 90, 72, 69 }, { 90, 72, 69, 78 }, { 90, 72, 69, 78, 71 }, { 90, 72, 73 }, { 83, 72, 73 }, { 90, 72, 73 }, { 90, 72, 79, 78, 71 }, { 90, 72, 79, 85 }, { 90, 72, 85 }, { 90, 72, 85, 65 }, { 90, 72, 85, 65, 73 }, { 90, 72, 85, 65, 78 }, { 90, 72, 85, 65, 78, 71 }, { 90, 72, 85, 73 }, { 90, 72, 85, 78 }, { 90, 72, 85, 79 }, { 90, 73 }, { 90, 79, 78, 71 }, { 90, 79, 85 }, { 90, 85 }, { 90, 85, 65, 78 }, { 90, 85, 73 }, { 90, 85, 78 }, { 90, 85, 79 }, new byte[6], { 83, 72, 65, 78 }, new byte[6] };
  private static final String FIRST_PINYIN_UNIHAN = "阿";
  private static final String LAST_PINYIN_UNIHAN = "鿿";
  private static final Collator COLLATOR = Collator.getInstance(Locale.CHINA);
  private static HanziToPinyin sInstance;
  private final boolean mHasChinaCollator;

  protected HanziToPinyin(boolean paramBoolean)
  {
    this.mHasChinaCollator = paramBoolean;
  }

  public static HanziToPinyin getInstance()
  {
    synchronized (HanziToPinyin.class)
    {
      if (sInstance != null)
        return sInstance;
      Locale[] arrayOfLocale = Collator.getAvailableLocales();
      for (int i = 0; i < arrayOfLocale.length; i++)
      {
        if ((!arrayOfLocale[i].equals(Locale.CHINA)) && ((!arrayOfLocale[i].getLanguage().equals("zh")) || (!arrayOfLocale[i].getCountry().equals("HANS"))))
          continue;
        sInstance = new HanziToPinyin(true);
        return sInstance;
      }
      EWoringLog.w("HanziToPinyin", "There is no Chinese collator, HanziToPinyin is disabled");
      sInstance = new HanziToPinyin(false);
      return sInstance;
    }
  }

  private static boolean doSelfValidation()
  {
    char c1 = UNIHANS[0];
    Object localObject = Character.toString(c1);
    for (char c2 : UNIHANS)
    {
      if (c1 == c2)
        continue;
      String str = Character.toString(c2);
      int k = COLLATOR.compare((String)localObject, str);
      if (k >= 0)
      {
        EWoringLog.e("HanziToPinyin", "Internal error in Unihan table. The last string \"" + (String)localObject + "\" is greater than current string \"" + str + "\".");
        return false;
      }
      localObject = str;
    }
    return true;
  }

  private Token getToken(char paramChar)
  {
    Token localToken = new Token();
    String str1 = Character.toString(paramChar);
    localToken.source = str1;
    int i = -1;
    if (paramChar < 'Ā')
    {
      localToken.type = 1;
      localToken.target = str1;
      return localToken;
    }
    int j = COLLATOR.compare(str1, "阿");
    if (j < 0)
    {
      localToken.type = 3;
      localToken.target = str1;
      return localToken;
    }
    if (j == 0)
    {
      localToken.type = 2;
      i = 0;
    }
    else
    {
      j = COLLATOR.compare(str1, "鿿");
      if (j > 0)
      {
        localToken.type = 3;
        localToken.target = str1;
        return localToken;
      }
      if (j == 0)
      {
        localToken.type = 2;
        i = UNIHANS.length - 1;
      }
    }
    localToken.type = 2;
    if (i < 0)
    {
      int k = 0;
      int m = UNIHANS.length - 1;
      while (k <= m)
      {
        i = (k + m) / 2;
        String str2 = Character.toString(UNIHANS[i]);
        j = COLLATOR.compare(str1, str2);
        if (j == 0)
          break;
        if (j > 0)
          k = i + 1;
        else
          m = i - 1;
      }
    }
    if (j < 0)
      i--;
    StringBuilder localStringBuilder = new StringBuilder();
    for (int m = 0; (m < PINYINS[i].length) && (PINYINS[i][m] != 0); m++)
      localStringBuilder.append((char)PINYINS[i][m]);
    localToken.target = localStringBuilder.toString();
    if (TextUtils.isEmpty(localToken.target))
    {
      localToken.type = 3;
      localToken.target = localToken.source;
    }
    return localToken;
  }

  public ArrayList<Token> get(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    if ((!this.mHasChinaCollator) || (TextUtils.isEmpty(paramString)))
      return localArrayList;
    int i = paramString.length();
    StringBuilder localStringBuilder = new StringBuilder();
    int j = 1;
    for (int k = 0; k < i; k++)
    {
    	char  m = paramString.charAt(k);
      if (m == 32)
      {
        if (localStringBuilder.length() <= 0)
          continue;
        addToken(localStringBuilder, localArrayList, j);
      }
      else if (m < 256)
      {
        if ((j != 1) && (localStringBuilder.length() > 0))
          addToken(localStringBuilder, localArrayList, j);
        j = 1;
        localStringBuilder.append(m);
      }
      else
      {
        Token localToken = getToken(m);
        if (localToken.type == 2)
        {
          if (localStringBuilder.length() > 0)
            addToken(localStringBuilder, localArrayList, j);
          localArrayList.add(localToken);
          j = 2;
        }
        else
        {
          if ((j != localToken.type) && (localStringBuilder.length() > 0))
            addToken(localStringBuilder, localArrayList, j);
          j = localToken.type;
          localStringBuilder.append(m);
        }
      }
    }
    if (localStringBuilder.length() > 0)
      addToken(localStringBuilder, localArrayList, j);
    return localArrayList;
  }

  private void addToken(StringBuilder paramStringBuilder, ArrayList<Token> paramArrayList, int paramInt)
  {
    String str = paramStringBuilder.toString();
    paramArrayList.add(new Token(paramInt, str, str));
    paramStringBuilder.setLength(0);
  }

  public static class Token
  {
    public static final String SEPARATOR = " ";
    public static final int LATIN = 1;
    public static final int PINYIN = 2;
    public static final int UNKNOWN = 3;
    public int type;
    public String source;
    public String target;

    public Token()
    {
    }

    public Token(int paramInt, String paramString1, String paramString2)
    {
      this.type = paramInt;
      this.source = paramString1;
      this.target = paramString2;
    }
  }
}


 